import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'controller.dart';

class StateGetxView extends StatelessWidget {
  StateGetxView({super.key});

  final controller = CountController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Getx"),
      ),
      body: Column(
        children: [
          GetX<CountController>(
              init: controller,
              initState: (_) {},
              builder: (_) {
                if (kDebugMode) {
                  print("GetX - 1");
                }
                return Text("value 1 ${_.count}");
              },
          ),
          GetX<CountController>(
            init: controller,
            initState: (_) {},
            builder: (_) {
              if (kDebugMode) {
                print("GetX - 2");
              }
              return Text('value 2 -> ${_.count}');
            },
          ),
          const Divider(),
          GetX<CountController>(
            init: controller,
            initState: (_) {},
            builder: (_) {
              if (kDebugMode) {
                print("GetX - 3");
              }
              return Column(
                children: [
                  Text('value 3 -> ${_.count}'),
                  ElevatedButton(
                    onPressed: () {
                      _.add();
                    },
                    child: const Text('count1'),
                  )
                ],
              );
            },
          ),
          const Divider(),
          // count2
          GetX<CountController>(
            init: controller,
            initState: (_) {},
            builder: (_) {
              if (kDebugMode) {
                print("GetX - 4");
              }
              return Text('value 4 -> ${_.count2}');
            },
          ),
          const Divider(),

          // 按钮
          ElevatedButton(
            onPressed: () {
              controller.add();
            },
            child: const Text('count1'),
          ),

          ElevatedButton(
            onPressed: () {
              controller.add2();
            },
            child: const Text('count2'),
          ),
        ],
      ),
    );
  }
}
